package com.example.controller;

import com.example.common.HttpStatusEnum;
import com.example.entity.ResponseData;
import com.example.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @email: pengyujun53@163.com
 * @author: peng_YuJun
 * @date: 2024/1/24
 * @time: 18:46
 */
@RestController
@RequestMapping("access")
@Api(tags = "用户登入登出请求类")
public class UserAccessController {

    @Resource
    private UserService userService;

    /**
     * 账号密码登录
     * @param phone
     * @param pwd
     * @return
     */
    @ApiOperation("账号密码登录")
    @PostMapping("loginUser")
    public ResponseData loginUser(String phone, String pwd) {
        if (!StringUtils.hasText(pwd) || !StringUtils.hasText(phone)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.loginUser(phone, pwd);
    }

    /**
     * 微信小程序用户登录
     * @param code
     * @return
     */
    @ApiOperation("微信小程序用户登录")
    @PostMapping("loginUserAtWMP")
    public ResponseData loginUserAtWMP(String code) {
        if (!StringUtils.hasText(code)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.loginUserAtWMP(code);
    }

    /**
     * 验证码短信获取
     * @param phone
     * @return
     */
    @ApiOperation("验证码短信获取")
    @GetMapping("getMsgCode")
    public ResponseData getMsgCode(String phone, HttpServletRequest request) {
        if (!StringUtils.hasText(phone)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(), HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.getMsgCode(phone, request);
    }

    /**
     * 短信验证码登录
     * @param phone
     * @param msgCode
     * @return
     */
    @ApiOperation("短信验证码登录")
    @PostMapping("msgCodeLogin")
    public ResponseData msgCodeLogin(String phone, String msgCode, HttpServletRequest request) {
        if (!StringUtils.hasText(msgCode) || !StringUtils.hasText(phone)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.msgCodeLogin(phone, msgCode, request);
    }

    /**
     * 短信修改密码
     * @param phone
     * @param newPwd
     * @param msgCode
     * @return
     */
    @Operation(summary = "短信修改密码")
    @PostMapping("resetUserPwdByMsg")
    @ResponseBody
    public ResponseData resetUserPwdByMsg(String phone, String newPwd, String msgCode, HttpServletRequest request){
        if (!StringUtils.hasText(msgCode) || !StringUtils.hasText(newPwd) || !StringUtils.hasText(phone)){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.resetUserPwdByMsg(phone, newPwd, msgCode, request);
    }

    /**
     * 修改密码
     * @param oldPwd
     * @param newPwd
     * @param id
     * @return
     */
    @Operation(summary = "修改密码")
    @PostMapping("resetUserPwd")
    @ResponseBody
    public ResponseData resetUserPwd(String oldPwd, String newPwd, Long id){
        if (!StringUtils.hasText(oldPwd) || !StringUtils.hasText(newPwd) || id == null){
            return new ResponseData(HttpStatusEnum.BAD_REQUEST.getCode(),HttpStatusEnum.BAD_REQUEST.getMessage());
        }
        return userService.resetUserPwd(oldPwd, newPwd, id);
    }

    /**
     * 系统登出
     * @param session
     * @return
     */
    @Operation(summary = "系统登出")
    @PostMapping("logOut")
    @ResponseBody
    public ResponseData loginOut(HttpSession session){
        //销毁session，同时若是有储存在redis中的共享session数据也会相应被删
        session.invalidate();
        return new ResponseData(HttpStatusEnum.SUCCESS.getCode(), "退出登录成功");
    }
}
